package platform.standard;


import engine.Constants;
import engine.Globals;
import engine.audio.AudioBuffer_I;
import engine.input.KeyQueue_I;
import engine.video.blit.PixelBuffer_I;
import platform.Platform_I;
import platform.standard.audio.JavaxAudio;
import platform.standard.input.AwtKeyQueue;
import platform.standard.video.AwtScreenBuffer;
import platform.standard.video.ScreenComponent;

public final class AwtPlatform implements Platform_I {
	public AwtPlatform() {

	}
	private MainWindow _AppWindow;
	private ScreenComponent _ScreenComponent;
	private JavaxAudio _AudioSystem;
	private AwtKeyQueue _KeyboardQueue;
	
	// Platform_A methods ------------------------------------------------------

	@Override
	public void initialize() {
		_AppWindow = new MainWindow();
		
		// Video Setup
		AwtScreenBuffer screenBuffer = new AwtScreenBuffer(
				Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
		//Globals.ScreenBuffer = screenBuffer;
		_ScreenComponent = new ScreenComponent(screenBuffer);
		_ScreenComponent.setSize(Constants.SCREEN_WIDTH,
				Constants.SCREEN_HEIGHT);
		_ScreenComponent.setFocusable(false);
		_AppWindow.add(_ScreenComponent);
		
		// Input setup.
		_KeyboardQueue = new AwtKeyQueue();
		// Not a big fan of this, but apparently keyboards only send input through Componenets.
		_AppWindow.addKeyListener(_KeyboardQueue);
		_AppWindow.requestFocus();

		// Audio Setup
		_AudioSystem = new JavaxAudio(Constants.AUDIO_LATENCY);
		if(_AudioSystem.IsInitialized == false) {
			_AudioSystem.shutdown();
			_AudioSystem = null;
		}
	}

	@Override
	public void shutDown() {
		_AppWindow.shutDown();
	}

	@Override
	public AudioBuffer_I getAudioBuffer() {
		if(_AudioSystem == null) {
			return null;
		}
		return _AudioSystem;
	}

	@Override
	public void playAudio() {
		if(_AudioSystem != null) {
			_AudioSystem.play();
		}
	}
	
	@Override
	public PixelBuffer_I getScreenBuffer() {
		return _ScreenComponent.getScreenBuffer();
	}
	
	@Override
	public void render() {
		_ScreenComponent.render();
	}

	@Override
	public long getTime() {
		return System.currentTimeMillis();
	}

	@Override
	public KeyQueue_I getKeyboard() {
		return _KeyboardQueue;
	}

}
